package xyz.zhzby.cyzx.oj.controller;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.zhzby.cyzx.oj.dto.request.ProblemAddRequest;
import xyz.zhzby.cyzx.oj.dto.request.ProblemJudgeRequest;
import xyz.zhzby.cyzx.oj.dto.request.ProblemPageRequest;
import xyz.zhzby.cyzx.oj.dto.response.ProblemResponse;
import xyz.zhzby.cyzx.oj.service.IProblemService;
import xyz.zhzby.cyzx.security.util.CyzxSecurityContextHolder;
import xyz.zhzby.cyzx.servicebase.domain.PageResponse;
import xyz.zhzby.cyzx.servicebase.domain.RestResponse;

/**
 * @author zby
 * @date 2021/12/26
 */
@RestController
@RequestMapping("/oj/problem")
@ApiModel("试题相关API")
public class ProblemController {

    @Autowired
    private IProblemService problemService;

    @ApiOperation("新增试题/草稿")
    @PostMapping("")
    public RestResponse<Long> createProblem(@RequestBody ProblemAddRequest problemAddRequest) {
        return problemService.createProblem(problemAddRequest);
    }


    @ApiOperation("更新试题/草稿")
    @PutMapping("")
    public RestResponse<Long> updateProblem(@RequestBody ProblemAddRequest problemAddRequest) {
        return problemService.updateProblem(problemAddRequest);
    }

    @ApiOperation("根据ID获取试题信息")
    @GetMapping("/get/{problemID}")
    public RestResponse<ProblemResponse> getProblemInfo(@PathVariable("problemID") Long problemID) {
        return problemService.getProblemById(problemID);
    }

    @ApiOperation("分页获取本人试题")
    @PostMapping("/list")
    public RestResponse<PageResponse<ProblemResponse>> getProblemList(@RequestBody ProblemPageRequest problemPageRequest) {
        problemPageRequest.setUserID(CyzxSecurityContextHolder.getSubject().getId());
        return problemService.getProblemList(problemPageRequest);
    }

    @ApiOperation("获取所有试题")
    @PostMapping("/all")
    public RestResponse<PageResponse<ProblemResponse>> getAllProblems(@RequestBody ProblemPageRequest problemPageRequest) {
        return problemService.getProblemList(problemPageRequest);
    }

    @ApiOperation("根据ID删除试题")
    @DeleteMapping("/{problemID}")
    public RestResponse<String> deleteProblem(@PathVariable("problemID") Long problemID) {
        return problemService.deleteProblem(problemID);
    }

    @ApiOperation("从回收站回收试题至草稿箱")
    @PostMapping("/recycle/{problemID}")
    public RestResponse<String> recycle(@PathVariable("problemID") Long problemID) {
        return problemService.recycle(problemID);
    }

    @ApiOperation("随机一题，返回随机试题的ID")
    @GetMapping("/random")
    public RestResponse<Long> getRandomProblem() {
        return problemService.getRandomProblem();
    }

    @ApiOperation("提交测评（执行代码）")
    @PostMapping("/test")
    public RestResponse<String> testCode(@RequestBody ProblemJudgeRequest problemJudgeRequest) {
        return problemService.testCode(problemJudgeRequest);
    }

    @ApiOperation("提交测评")
    @PostMapping("/judge")
    public RestResponse<String> judgeCode(@RequestBody ProblemJudgeRequest problemJudgeRequest) {
        return problemService.judgeCode(problemJudgeRequest);
    }

}
